package com.wash.shoes.util;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Component;

import com.ruoyi.common.utils.StringUtils;
import com.wash.shoes.entity.ShopPo;
import com.wash.shoes.mapper.UserShopMapper;

import cn.hutool.core.collection.CollectionUtil;

/**
 * @author lijianguo
 * @Date 22/09/2024 请输入类的简介
 **/
@Component
public class HeaderUtil {

    private static HttpServletRequest httpServletRequestStic;
    private static UserShopMapper userShopMapperStic;
    @Resource
    HttpServletRequest httpServletRequest;
    @Resource
    UserShopMapper userShopMapper;

    public static String getShopId() {
        String shopId = httpServletRequestStic.getHeader("Currentshopid");
        return shopId;
    }

    /**
     * @author lijianguo
     * @Date 14/10/2024 shop
     **/
    public static String getShopIdStr() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if(user.hasAdmin()){
            return null;
        }
        List<String> userBindTypeList = userShopMapperStic.getUserBindType(String.valueOf(AdminUserUtil.getUserId()));
        String userBindType = "";
        List<String> shopIds = Lists.newArrayList();
        if (CollectionUtil.isNotEmpty(userBindTypeList)) {
            userBindType = userBindTypeList.get(0);
        }
        if (StringUtils.isNotEmpty(userBindType)) {
            // 如果是店铺类型 就取
            if ("shop".equals(userBindType)) {
                List<ShopPo> dataList = getShopList();
                shopIds = dataList.stream().map(ShopPo::getId).distinct().collect(Collectors.toList());
            } else {
                // 如果是工厂类型就取绑定的工厂
                shopIds = userShopMapperStic.queryShopIdByUserId(String.valueOf(AdminUserUtil.getUserId()));
            }
        }

        StringBuilder sb = new StringBuilder();
        sb.append("(");
        sb.append(String.join(",", shopIds));
        sb.append(")");
        if (shopIds.isEmpty()) {
            return null;
        } else {
            return sb.toString();
        }
    }

    public static List<ShopPo> getShopList() {
        return userShopMapperStic.userAllShopList(AdminUserUtil.getUserId());
    }

    /**
     * 获取抖音小程序前端传递得门店id
     *
     * @return
     */
    public static String getPoId() {
        return httpServletRequestStic.getHeader("poid");
    }

    @PostConstruct
    void init() {
        httpServletRequestStic = httpServletRequest;
        userShopMapperStic = userShopMapper;
    }

}
